package sf.database.util;

import sf.core.DBObject;
import sf.database.OrmConfig;
import sf.database.meta.ColumnMapping;
import sf.database.meta.MetaHolder;
import sf.spring.util.ReflectionUtils;
import sf.tools.reflectasm.ConstructorAccess;

import java.lang.reflect.Field;

/**
 * orm的设置值的方法
 */
public class OrmValueUtils {


    /**
     * 设置值的方法.
     */
    public enum BeanValueType {
        /**
         * 按字段
         */
        field,
        /**
         * 按方法(默认)
         */
        method,
        /**
         * 按reflectasm方法
         */
        reflectasm,
        /**
         * 快速,使用pull和push(暂时未实现)
         */
        fast,
        /**
         * 其他方法(备用,未实现)
         */
        other
    }

    /**
     * 实例化,提供快速实例化,或标准实例化
     * @param clz
     * @param <T>
     * @return
     */
    public static <T> T instance(Class<T> clz) {
        T t = null;
        switch (OrmConfig.getInstance().getBeanValueType()) {
            case reflectasm:
                t = ConstructorAccess.get(clz).newInstance();
                break;
            default:
                try {
                    t = clz.newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                break;

        }
        return t;
    }

    /**
     * 设置值
     * @param obj
     * @param cm
     * @param value
     * @param <T>
     */
    public static <T> void setValue(T obj, ColumnMapping cm, Object value) {
        setValue(obj, cm, value, OrmConfig.getInstance().getBeanValueType());
    }

    /**
     * @param obj
     * @param cm
     * @param value
     * @param beanValueType
     * @param <T>
     */
    public static <T> void setValue(T obj, ColumnMapping cm, Object value, BeanValueType beanValueType) {
        switch (beanValueType) {
            case reflectasm:
                setValueMethodReflectasm(obj, cm, value);
                break;
            case field:
                setValueFieldReflect(obj, cm, value);
                break;
            case fast:
                if (DBObject.class.isAssignableFrom(cm.getMeta().getThisType()) && cm.getMeta().isExistPushMethod()) {
                    DBObject o = (DBObject) obj;
                    setValueMethodFast(o, cm, value);
                }
            case method:
            default:
                setValueMethodReflect(obj, cm, value);
                break;
        }
    }

    /**
     * 使用field 反射设置值
     * @param obj
     * @param cm
     * @param value
     * @param <T>
     */
    private static <T> void setValueFieldReflect(T obj, ColumnMapping cm, Object value) {
        Field f = cm.getFieldAccessor().getField();
        if (!f.isAccessible()) {
            f.setAccessible(true);
        }
        ReflectionUtils.setField(cm.getFieldAccessor().getField(), obj, value);
    }

    /**
     * 使用method 反射设置值
     * @param obj
     * @param cm
     * @param value
     * @param <T>
     */
    private static <T> void setValueMethodReflect(T obj, ColumnMapping cm, Object value) {
        cm.getFieldAccessor().set(obj, value);
    }

    /**
     * @param obj
     * @param cm
     * @param value
     * @param <T>
     */
    private static <T> void setValueMethodReflectasm(T obj, ColumnMapping cm, Object value) {
//        MethodAccess methodAccess = cm.getMeta().getMethodAccess();
        cm.getMeta().getMethodAccess().invoke(obj, cm.getFieldAccessor().getReflectasmSetterIndex(), value);
    }

    /**
     * @param obj
     * @param cm
     * @param value
     * @param <T>
     */
    private static <T extends DBObject> void setValueMethodFast(T obj, ColumnMapping cm, Object value) {
        obj.push(cm.getFieldName(), value);
    }

    /**
     * @param obj
     * @param cm
     * @param <T>
     * @param <U>
     * @return
     */
    public static <T, U> U getValue(T obj, ColumnMapping cm) {
        return getValue(obj, cm, OrmConfig.getInstance().getBeanValueType());
    }

    /**
     * @param obj
     * @param cm
     * @param beanValueType
     * @param <T>
     * @param <U>
     * @return
     */
    public static <T, U> U getValue(T obj, ColumnMapping cm, BeanValueType beanValueType) {
        if (MetaHolder.getMeta(obj.getClass()) != cm.getMeta()) {
            throw new RuntimeException("类型不一致");
        }

        switch (beanValueType) {
            case reflectasm:
                return getValueMethodReflectasm(obj, cm);
            case field:
                return getValueFieldReflect(obj, cm);
            case fast:
                if (DBObject.class.isAssignableFrom(cm.getMeta().getThisType()) && cm.getMeta().isExistPullMethod()) {
                    DBObject o = (DBObject) obj;
                    return getValueMethodFast(o, cm);
                }
            case method:
            default:
                return getValueMethodReflect(obj, cm);
        }
    }

    /**
     * @param obj
     * @param cm
     * @param <T>
     * @param <U>
     * @return
     */
    private static <T, U> U getValueFieldReflect(T obj, ColumnMapping cm) {
        Field f = cm.getFieldAccessor().getField();
        if (!f.isAccessible()) {
            f.setAccessible(true);
        }
        Object value = ReflectionUtils.getField(f, obj);
        return (U) value;
    }

    /**
     * @param obj
     * @param cm
     * @param <T>
     * @param <U>
     * @return
     */
    private static <T, U> U getValueMethodReflect(T obj, ColumnMapping cm) {
        Object value = cm.getFieldAccessor().get(obj);
        return (U) value;
    }

    /**
     * @param obj
     * @param cm
     * @param <T>
     * @param <U>
     * @return
     */
    private static <T, U> U getValueMethodReflectasm(T obj, ColumnMapping cm) {
//        MethodAccess methodAccess = cm.getMeta().getMethodAccess();
        Object value = cm.getMeta().getMethodAccess().invoke(obj, cm.getFieldAccessor().getReflectasmGetterIndex());
        return (U) value;
    }

    /**
     * @param obj
     * @param cm
     * @param <T>
     * @param <U>
     * @return
     */
    private static <T extends DBObject, U> U getValueMethodFast(T obj, ColumnMapping cm) {
        Object value = obj.pull(cm.getFieldName());
        return (U) value;
    }
}
